package com.mb.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mb.app.domain.TbOrder;
import com.mb.app.domain.TbProductInfo;
import com.mb.app.domain.TbShipCfg;
import com.mb.app.service.IAddrService;
import com.mb.app.service.ITbMemberInfoService;
import com.mb.app.service.ITbOrderService;
import com.mb.app.service.ITbShipCfgService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import com.mb.ship.service.IShipService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表  前端控制器
 * </p>
 *
 * @author liliping
 * @since 2019-04-15
 */
@Controller
@RequestMapping("/app/order")
@Slf4j
public class TbOrderController extends BaseController {

    @Autowired
    private ITbOrderService orderService ;

    @Autowired
    private ITbMemberInfoService memberInfoService;

    @Autowired
    private ITbShipCfgService shipCfgService;

    @Autowired
    private IShipService shipService;

    @Autowired
    private IAddrService addrService;

    @GetMapping("/index")
    String index(){
        return "/app/order/order_list";
    }

    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbProductInfo> list = orderService.queryList(query);
        int total = orderService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    /**
     * 详情
     * @param orderId
     * @param model
     * @return
     */
    @GetMapping("/dtl/{orderId}")
    String dtl(@PathVariable("orderId") String  orderId, Model model){
        //获取订单详细数据
        TbOrder order = orderService.selectById(orderId);
        if (order != null) {
            String addr = addrService.getAddrName(order.getReceiveAreaId());
            order.setDtlAddr(addr + order.getDtlAddr());
            //获取订单人姓名
            String nickName = memberInfoService.selectById(order.getMemberId()).getNickName();
            //转换日期格式
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createTime = formatter.format(order.getCreateTime());
            //获取所有物流公司信息
            List<TbShipCfg> shipCfgs = shipCfgService.selectAll();
            model.addAttribute("createTime",createTime);
            model.addAttribute("nickName",nickName);
            model.addAttribute("order",order);
            model.addAttribute("shipCfgs",shipCfgs);
        }
        return "/app/order/order_info";
    }


    @ResponseBody
    @GetMapping("/productList")
    public PageUtils productList(@RequestParam Map<String, Object> params) {
        String orderId = (String)params.get("orderId");
        List<TbProductInfo> products =  orderService.getProducts(orderId);
        PageUtils pageUtils = new PageUtils(products,products.size());
        return pageUtils;
    }

    /**
     * 发货
     * @param tbOrder
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public R update(TbOrder tbOrder){
        tbOrder.setOrderStatus(2);
        tbOrder.setShipTime(new Date());
        boolean b = orderService.updateById(tbOrder);
        return R.ok();
    }

    /**
     * 查看物流
     * @param shipTraceNo
     * @param model
     * @return
     */
    @GetMapping("/ship/{shipTraceNo}")
    String ship(@PathVariable("shipTraceNo") String  shipTraceNo, Model model){
        JSONObject resp = new JSONObject();
        resp.put("no",shipTraceNo);
        JSONObject jsonObject = shipService.shipQuery(resp);
        if (jsonObject != null){
            String result = jsonObject.getString("result");
            Map map = JSON.parseObject(result, Map.class);
            Object list = map.get("list");
            model.addAttribute("ships",list);
        }
        return "/app/order/order_ship";
    }

    /**
     * 删除
     */
    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("orderId") String orderId, HttpServletRequest request){
        TbOrder order = new TbOrder();
        order.setDataStatus(0);
        order.setOrderId(orderId);
        boolean b = orderService.updateById(order);
        if (b){
            return R.ok("成功");
        }
        return R.ok("失败");
    }

}
